package br.com.educa.modelo.academico;

import br.com.educa.modelo.academico.enumerator.Turno;
import br.com.educa.modelo.pessoa.Pessoas;
import flexjson.JSONDeserializer;
import flexjson.JSONSerializer;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityManager;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.PersistenceContext;
import javax.persistence.Table;
import javax.persistence.TypedQuery;
import javax.persistence.Version;
import javax.validation.constraints.NotNull;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.jpa.activerecord.RooJpaActiveRecord;
import org.springframework.roo.addon.json.RooJson;
import org.springframework.roo.addon.serializable.RooSerializable;
import org.springframework.roo.addon.tostring.RooToString;
import org.springframework.transaction.annotation.Transactional;

@Entity
@Table
@Configurable
@RooJson
@RooSerializable
@RooJavaBean
@RooToString
@RooJpaActiveRecord(finders = { "findTurmasesByAnoLetivoEquals" })
public class Turmas implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id")
    private Long id;

    @Version
    @Column(name = "version")
    private Integer version;

    @ManyToOne
    @JoinColumn(referencedColumnName = "id")
    private Cursos curso;

    @ManyToOne
    @JoinColumn(referencedColumnName = "id")
    private Salas sala;

    @ManyToOne
    @JoinColumn(referencedColumnName = "id")
    private Alunos chefeTurma;

    @ManyToOne
    @JoinColumn(referencedColumnName = "id")
    private Pessoas padrinho;

    @NotNull
    private String turma;

    private String nomeTurma;

    @Enumerated(EnumType.STRING)
    private Turno turno;

    private Integer vagas;

    private Double valorContribuicao;

    private Double valorMatricula;

    private Integer anoLetivo;

    private String periodo;

	private Boolean ativa;

    @PersistenceContext
    transient EntityManager entityManager;

    private static final long serialVersionUID = 1L;

    public String toJson() {
        return new JSONSerializer().exclude("*.class").serialize(this);
    }

    public static br.com.educa.modelo.academico.Turmas fromJsonToTurmas(String json) {
        return new JSONDeserializer<Turmas>().use(null, Turmas.class).deserialize(json);
    }

    public static String toJsonArray(Collection<br.com.educa.modelo.academico.Turmas> collection) {
        return new JSONSerializer().exclude("*.class").serialize(collection);
    }

    public static Collection<br.com.educa.modelo.academico.Turmas> fromJsonArrayToTurmases(String json) {
        return new JSONDeserializer<List<Turmas>>().use(null, ArrayList.class).use("values", Turmas.class).deserialize(json);
    }

    public static final EntityManager entityManager() {
        EntityManager em = new Turmas().entityManager;
        if (em == null) throw new IllegalStateException("Entity manager has not been injected (is the Spring Aspects JAR configured as an AJC/AJDT aspects library?)");
        return em;
    }

    public static long countTurmases() {
        return entityManager().createQuery("SELECT COUNT(o) FROM Turmas o", Long.class).getSingleResult();
    }

    public static List<br.com.educa.modelo.academico.Turmas> findAllTurmases() {
        return entityManager().createQuery("SELECT o FROM Turmas o", Turmas.class).getResultList();
    }

    public static br.com.educa.modelo.academico.Turmas findTurmas(Long id) {
        if (id == null) return null;
        return entityManager().find(Turmas.class, id);
    }

    public static List<br.com.educa.modelo.academico.Turmas> findTurmasEntries(int firstResult, int maxResults) {
        return entityManager().createQuery("SELECT o FROM Turmas o", Turmas.class).setFirstResult(firstResult).setMaxResults(maxResults).getResultList();
    }

    @Transactional
    public void persist() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.persist(this);
    }

    @Transactional
    public void remove() {
        if (this.entityManager == null) this.entityManager = entityManager();
        if (this.entityManager.contains(this)) {
            this.entityManager.remove(this);
        } else {
            Turmas attached = Turmas.findTurmas(this.id);
            this.entityManager.remove(attached);
        }
    }

    @Transactional
    public void flush() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.flush();
    }

    @Transactional
    public void clear() {
        if (this.entityManager == null) this.entityManager = entityManager();
        this.entityManager.clear();
    }

    @Transactional
    public br.com.educa.modelo.academico.Turmas merge() {
        if (this.entityManager == null) this.entityManager = entityManager();
        Turmas merged = this.entityManager.merge(this);
        this.entityManager.flush();
        return merged;
    }

    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Integer getVersion() {
        return this.version;
    }

    public void setVersion(Integer version) {
        this.version = version;
    }

    public Cursos getCurso() {
        return this.curso;
    }

    public void setCurso(Cursos curso) {
        this.curso = curso;
    }

    public Salas getSala() {
        return this.sala;
    }

    public void setSala(Salas sala) {
        this.sala = sala;
    }

    public Alunos getChefeTurma() {
        return this.chefeTurma;
    }

    public void setChefeTurma(Alunos chefeTurma) {
        this.chefeTurma = chefeTurma;
    }

    public Pessoas getPadrinho() {
        return this.padrinho;
    }

    public void setPadrinho(Pessoas padrinho) {
        this.padrinho = padrinho;
    }

    public String getTurma() {
        return this.turma;
    }

    public void setTurma(String turma) {
        this.turma = turma;
    }

    public String getNomeTurma() {
        return this.nomeTurma;
    }

    public void setNomeTurma(String nomeTurma) {
        this.nomeTurma = nomeTurma;
    }

    public Turno getTurno() {
        return this.turno;
    }

    public void setTurno(Turno turno) {
        this.turno = turno;
    }

    public Integer getVagas() {
        return this.vagas;
    }

    public void setVagas(Integer vagas) {
        this.vagas = vagas;
    }

    public Double getValorContribuicao() {
        return this.valorContribuicao;
    }

    public void setValorContribuicao(Double valorContribuicao) {
        this.valorContribuicao = valorContribuicao;
    }

    public Double getValorMatricula() {
        return this.valorMatricula;
    }

    public void setValorMatricula(Double valorMatricula) {
        this.valorMatricula = valorMatricula;
    }

    public Integer getAnoLetivo() {
        return this.anoLetivo;
    }

    public void setAnoLetivo(Integer anoLetivo) {
        this.anoLetivo = anoLetivo;
    }

    public String getPeriodo() {
        return this.periodo;
    }

    public void setPeriodo(String periodo) {
        this.periodo = periodo;
    }

    public Boolean getAtiva() {
        return this.ativa;
    }

    public void setAtiva(Boolean ativa) {
        this.ativa = ativa;
    }

    public String toString() {
        return ReflectionToStringBuilder.toString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

	public static TypedQuery<Turmas> findTurmasesByAnoLetivoEquals(Integer anoLetivo) {
        if (anoLetivo == null) throw new IllegalArgumentException("The anoLetivo argument is required");
        EntityManager em = Turmas.entityManager();
        TypedQuery<Turmas> q = em.createQuery("SELECT o FROM Turmas AS o WHERE o.anoLetivo = :anoLetivo", Turmas.class);
        q.setParameter("anoLetivo", anoLetivo);
        return q;
    }
}
